package o;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.appgallery.splashscreen.impl.cache.FragmentCacheBean;
import com.huawei.appgallery.splashscreen.impl.cache.ImageCacheBean;
import com.huawei.appgallery.splashscreen.impl.server.FestivalImageResponseBean.StartImageInfo;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import o.fus.a;

public class cyt implements Runnable {
    private int ˋ;
    private List<StartImageInfo> ˏ;

    public cyt(List<StartImageInfo> list, int i) {
        this.ˏ = list;
        this.ˋ = i;
    }

    public void run() {
        if (this.ˏ == null || this.ˏ.isEmpty()) {
            cyq.ˋ.ˋ("GetStartImageTask", "GetStartImageTask imageInfos is empty.");
            cys.ˎ();
            cyx.ˎ();
            return;
        }
        if (cyx.ˋ()) {
            cys.ˎ();
            cyx.ˎ();
            cyx.ˊ();
        }
        String ˋ = cys.ˋ();
        if (ˋ != null) {
            int i = 0;
            for (StartImageInfo startImageInfo : this.ˏ) {
                try {
                    if (startImageInfo.ॱ() > System.currentTimeMillis()) {
                        ˊ(startImageInfo, ˋ, i);
                        i++;
                    } else {
                        cyq.ˋ.ˋ("GetStartImageTask", "The event is over.");
                    }
                } catch (Exception e) {
                    cyq.ˋ.ˏ("GetStartImageTask", "downloadStartImage error : " + e.getMessage());
                }
            }
            cyx.ˏ(i);
        }
    }

    private boolean ˏ(String str, String str2) {
        fvb execute;
        FileOutputStream fileOutputStream;
        InputStream bufferedInputStream;
        OutputStream bufferedOutputStream;
        int i;
        Throwable th;
        FileOutputStream fileOutputStream2;
        Throwable th2;
        Exception e;
        fvb o_fvb;
        Throwable th3;
        boolean z = false;
        OutputStream outputStream = null;
        if (!TextUtils.isEmpty(str)) {
            try {
                fuv ॱ = dnb.ॱ();
                a ˎ = new a().ˎ(str);
                ˎ.ˋ("Accept-Encoding", "identity");
                execute = ॱ.ˎ(ˎ.ॱ()).execute();
                try {
                    cyq.ˋ.ˎ("GetStartImageTask", "downloadUrlToStream status code:" + execute.ˊ());
                    if (execute.ˏ()) {
                        fuy ᐝ = execute.ᐝ();
                        long ˏ = ᐝ.ˏ();
                        try {
                            fileOutputStream = new FileOutputStream(new File(str2));
                            try {
                                bufferedInputStream = new BufferedInputStream(ᐝ.ˎ(), 8192);
                                try {
                                    bufferedOutputStream = new BufferedOutputStream(fileOutputStream, 8192);
                                } catch (FileNotFoundException e2) {
                                    i = 0;
                                    bufferedOutputStream = null;
                                    try {
                                        cyq.ˋ.ˊ("GetStartImageTask", "download featival image FileNotFoundException error:");
                                        ॱ(execute, bufferedInputStream, bufferedOutputStream);
                                        if (fileOutputStream != null) {
                                            fileOutputStream.close();
                                        }
                                        if (ˏ == ((long) i)) {
                                            z = true;
                                        }
                                        ॱ(execute, null, null);
                                        return z;
                                    } catch (Throwable th4) {
                                        th = th4;
                                        fileOutputStream2 = fileOutputStream;
                                        th2 = th;
                                        ॱ(execute, bufferedInputStream, bufferedOutputStream);
                                        if (fileOutputStream2 != null) {
                                            fileOutputStream2.close();
                                        }
                                        throw th2;
                                    }
                                } catch (Throwable th42) {
                                    bufferedOutputStream = null;
                                    FileOutputStream fileOutputStream3 = fileOutputStream;
                                    th2 = th42;
                                    fileOutputStream2 = fileOutputStream3;
                                    ॱ(execute, bufferedInputStream, bufferedOutputStream);
                                    if (fileOutputStream2 != null) {
                                        fileOutputStream2.close();
                                    }
                                    throw th2;
                                }
                                try {
                                    byte[] bArr = new byte[8192];
                                    i = 0;
                                    while (true) {
                                        try {
                                            int read = bufferedInputStream.read(bArr);
                                            if (read != -1) {
                                                bufferedOutputStream.write(bArr, 0, read);
                                                i += read;
                                            } else {
                                                try {
                                                    break;
                                                } catch (Exception e3) {
                                                    e = e3;
                                                    o_fvb = execute;
                                                    try {
                                                        cyq.ˋ.ˏ("GetStartImageTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
                                                        ॱ(o_fvb, bufferedInputStream, bufferedOutputStream);
                                                        return z;
                                                    } catch (Throwable th5) {
                                                        th3 = th5;
                                                        execute = o_fvb;
                                                        outputStream = bufferedOutputStream;
                                                        ॱ(execute, bufferedInputStream, outputStream);
                                                        throw th3;
                                                    }
                                                } catch (Throwable th6) {
                                                    th3 = th6;
                                                    outputStream = bufferedOutputStream;
                                                    ॱ(execute, bufferedInputStream, outputStream);
                                                    throw th3;
                                                }
                                            }
                                        } catch (FileNotFoundException e4) {
                                        }
                                    }
                                    ॱ(execute, bufferedInputStream, bufferedOutputStream);
                                    if (fileOutputStream != null) {
                                        fileOutputStream.close();
                                    }
                                } catch (FileNotFoundException e5) {
                                    i = 0;
                                    cyq.ˋ.ˊ("GetStartImageTask", "download featival image FileNotFoundException error:");
                                    ॱ(execute, bufferedInputStream, bufferedOutputStream);
                                    if (fileOutputStream != null) {
                                        fileOutputStream.close();
                                    }
                                    if (ˏ == ((long) i)) {
                                        z = true;
                                    }
                                    ॱ(execute, null, null);
                                    return z;
                                }
                            } catch (FileNotFoundException e6) {
                                i = 0;
                                bufferedOutputStream = null;
                                bufferedInputStream = null;
                                cyq.ˋ.ˊ("GetStartImageTask", "download featival image FileNotFoundException error:");
                                ॱ(execute, bufferedInputStream, bufferedOutputStream);
                                if (fileOutputStream != null) {
                                    fileOutputStream.close();
                                }
                                if (ˏ == ((long) i)) {
                                    z = true;
                                }
                                ॱ(execute, null, null);
                                return z;
                            } catch (Throwable th422) {
                                bufferedOutputStream = null;
                                bufferedInputStream = null;
                                th = th422;
                                fileOutputStream2 = fileOutputStream;
                                th2 = th;
                                ॱ(execute, bufferedInputStream, bufferedOutputStream);
                                if (fileOutputStream2 != null) {
                                    fileOutputStream2.close();
                                }
                                throw th2;
                            }
                        } catch (FileNotFoundException e7) {
                            fileOutputStream = null;
                            i = 0;
                            bufferedOutputStream = null;
                            bufferedInputStream = null;
                            cyq.ˋ.ˊ("GetStartImageTask", "download featival image FileNotFoundException error:");
                            ॱ(execute, bufferedInputStream, bufferedOutputStream);
                            if (fileOutputStream != null) {
                                fileOutputStream.close();
                            }
                            if (ˏ == ((long) i)) {
                                z = true;
                            }
                            ॱ(execute, null, null);
                            return z;
                        } catch (Throwable th7) {
                            th2 = th7;
                            fileOutputStream2 = null;
                            bufferedOutputStream = null;
                            bufferedInputStream = null;
                            ॱ(execute, bufferedInputStream, bufferedOutputStream);
                            if (fileOutputStream2 != null) {
                                fileOutputStream2.close();
                            }
                            throw th2;
                        }
                        if (ˏ == ((long) i)) {
                            z = true;
                        }
                        ॱ(execute, null, null);
                    } else {
                        ॱ(execute, null, null);
                    }
                } catch (Exception e8) {
                    e = e8;
                    bufferedOutputStream = null;
                    bufferedInputStream = null;
                    o_fvb = execute;
                    cyq.ˋ.ˏ("GetStartImageTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
                    ॱ(o_fvb, bufferedInputStream, bufferedOutputStream);
                    return z;
                } catch (Throwable th8) {
                    th3 = th8;
                    bufferedInputStream = null;
                    ॱ(execute, bufferedInputStream, outputStream);
                    throw th3;
                }
            } catch (Exception e9) {
                e = e9;
                bufferedOutputStream = null;
                bufferedInputStream = null;
                cyq.ˋ.ˏ("GetStartImageTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
                ॱ(o_fvb, bufferedInputStream, bufferedOutputStream);
                return z;
            } catch (Throwable th9) {
                th3 = th9;
                execute = null;
                bufferedInputStream = null;
                ॱ(execute, bufferedInputStream, outputStream);
                throw th3;
            }
        }
        return z;
    }

    private void ॱ(fvb o_fvb, InputStream inputStream, OutputStream outputStream) {
        cfn.ˎ(inputStream);
        cfn.ˎ(outputStream);
        cfn.ˎ(o_fvb);
    }

    private String ˊ(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return str3 + (str2 + str.substring(str.lastIndexOf("/") + 1, str.length()));
    }

    private void ˊ(StartImageInfo startImageInfo, String str, int i) {
        if (startImageInfo != null) {
            cyv ॱ = cyx.ॱ(i);
            if (ॱ == null) {
                ॱ = new cyv();
            }
            ImageCacheBean ॱ2 = ॱ(startImageInfo, ॱ, str);
            FragmentCacheBean ˊ = ˊ(startImageInfo, ॱ);
            ॱ2.ॱ(this.ˋ);
            ॱ.ˊ(ॱ2);
            ॱ.ˏ(ˊ);
            cyx.ॱ(ॱ, i);
        }
    }

    private ImageCacheBean ॱ(StartImageInfo startImageInfo, cyv o_cyv, String str) {
        ImageCacheBean ॱ = o_cyv.ॱ();
        ॱ.ˋ(startImageInfo.ˏ());
        ॱ.ॱ(startImageInfo.ॱ());
        ॱ.ˊ(startImageInfo.ʽ());
        ॱ.ॱ(startImageInfo.ᐝ());
        ॱ.ˏ(startImageInfo.ͺ());
        ॱ.ˏ(startImageInfo.ॱॱ());
        ॱ.ˊ(startImageInfo.ˊ());
        ॱ.ˋ(startImageInfo.ˊॱ());
        String ˊ = ˊ(startImageInfo.ʼ(), "startImageHor", str);
        String ˊ2 = ˊ(startImageInfo.ˋ(), "startImageVer", str);
        ॱ.ˋ(ˊ);
        ॱ.ˎ(ˊ2);
        ˏ(ˊ, startImageInfo.ʽ(), startImageInfo.ʼ(), startImageInfo.ˊ());
        ˏ(ˊ2, startImageInfo.ᐝ(), startImageInfo.ˋ(), startImageInfo.ˊ());
        ˊ = o_cyv.ˏ().ᐝ();
        ˊ2 = startImageInfo.ॱˊ();
        if (!(ˊ == null || ˊ2 == null || ˊ.equals(ˊ2))) {
            ॱ.ˊ(-1);
            ॱ.ˎ(0);
            ॱ.ˎ(-1);
        }
        return ॱ;
    }

    private FragmentCacheBean ˊ(StartImageInfo startImageInfo, cyv o_cyv) {
        FragmentCacheBean ˏ = o_cyv.ˏ();
        ˏ.ˎ(startImageInfo.ʻ());
        ˏ.ˏ(startImageInfo.ˎ());
        ˏ.ˋ(startImageInfo.ˏॱ());
        ˏ.ˏ(startImageInfo.ˋॱ());
        ˏ.ॱ(startImageInfo.ॱˊ());
        ˏ.ˊ(startImageInfo.ˊ());
        ˏ.ˏ(startImageInfo.ॱˎ());
        ˏ.ˋ(startImageInfo.ॱˋ());
        return ˏ;
    }

    private void ˏ(String str, String str2, String str3, int i) {
        if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2) && !TextUtils.isEmpty(str3)) {
            File file = new File(str);
            if (file.exists()) {
                if (str2.equalsIgnoreCase(cfn.ˏ(str, AaidIdConstant.SIGNATURE_SHA256))) {
                    cyq.ˋ.ˋ("GetStartImageTask", "Login Image SHA-256 check success.url: " + str3);
                    return;
                }
                cyq.ˋ.ˋ("GetStartImageTask", "Login Image SHA-256 check failed.");
                if (!file.delete()) {
                    cyq.ˋ.ˋ("GetStartImageTask", "isExitsFile delete error.");
                }
            }
            if (2 != i || ˏ(cyn.ॱ())) {
                try {
                    boolean ˏ = ˏ(str3, str);
                    cyq.ˋ.ˋ("GetStartImageTask", "download festival image:" + str3 + "，success? :" + ˏ);
                    if (!ˏ) {
                        file = new File(str);
                        if (file.exists() && !file.delete()) {
                            cyq.ˋ.ˋ("GetStartImageTask", "file delete error.");
                        }
                    }
                } catch (Exception e) {
                    cyq.ˋ.ˊ("GetStartImageTask", "download featival image Exception error:" + e.toString());
                }
            }
        }
    }

    private boolean ˏ(Context context) {
        return doo.ॱॱ(context) && !doo.ᐝ(context);
    }
}
